<?php
namespace App\Adshopuser\Models;
use Common\BaseModel;
use Common\Common;

class BShopGoodsComment extends BaseModel
{
    public $res;
    public $expire;

    public function initialize()
    {
        parent::initialize();
        $this->setSource('b_shop_goods_comment');
    }

    public function get_list($params=[]){
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condtions="g.shop_id={$params['shop_id']}";
        $sql="
        SELECT c.id,c.content,c.img,c.star,c.descript_star,c.express_star,c.service_star,g.name,u.nickname,u.avatar,u.phone,c.created_at FROM `b_shop_goods_comment` as c
left join b_shop_goods as g on c.goods_id=g.id
left join f_users as u on u.id=c.user_id
where $condtions
group by c.id DESC
limit $page,$size";
        $items = $this->getDI()->get('dbMaster')->fetchAll($sql);
        foreach($items as $key=>$value){
            $items[$key]['star'] = (int)$value['star'];
            $items[$key]['descript_star'] = (int)$value['descript_star'];
            $items[$key]['express_star'] = (int)$value['express_star'];
            $items[$key]['service_star'] = (int)$value['service_star'];
            $items[$key]['nickname'] = $value['nickname'] ? $value['nickname'] : substr_replace($value['phone'], '****', 3, 4);
            $items[$key]['avatar'] = $value['avatar'] ? $value['avatar'] : Common::AVATAR_DEFAULT;
            $items[$key]['img'] = $value['img'] ? explode(',',$value['img']) : [];
            unset($items[$key]['phone']);
        }
        $get_list['items'] = $items;



        $count_sql="
        SELECT count(c.id) as count FROM `b_shop_goods_comment` as c
left join b_shop_goods as g on c.goods_id=g.id
left join f_users as u on u.id=c.user_id
where $condtions";
        $g_count = $this->getDI()->get('dbMaster')->fetchOne($count_sql);
        $get_list['total'] = isset($g_count['count']) && !empty($g_count['count']) ? (int)$g_count['count'] : 0;
        return $get_list;
    }

}
